package model

import (
	"gorm.io/gorm"
	"strconv"
	"test-demo/common"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(50)"`
	Password string `gorm:"type:varchar(50)"`
	RealName string `gorm:"type:varchar(50)"`
	IdCode   string `gorm:"type:char(18)"`
}

func Login(username string, password string) string {
	u := User{}
	db.Where("username", username).Find(&u)
	if u.ID == 0 {
		return "0"
	}
	if u.Password != password {
		return "0"
	}
	return strconv.Itoa(int(u.ID))
}

func RealTest(userid int, realname string, idcode string) string {
	str := common.Real(realname, idcode)
	if str != "一致" {
		return str
	}
	u := User{}
	db.Where("id", userid).Find(&u)
	u.RealName = realname
	u.IdCode = idcode
	u.ID = uint(userid)
	db.Save(&u)
	return strconv.Itoa(int(u.ID))
}
